Codewars + задание на классы#14
Codewars + задание на классы#14Rey1147 wants to merge 4 commits intoISUCT:Midjaeva_Evgenija_Gennadevnafrom
Conversation
rpgsaga/saga/package.json
Outdated
| "main": "index.js", | ||
| "scripts": { | ||
| "dev": "ts-node --script-mode src/index.ts", | ||
| "dev": "ts-node --script-mode src/gun.ts", |
There was a problem hiding this comment.
лучше оставить как было - запуск - это запуск основного файла в проекте
| @@ -0,0 +1,42 @@ | |||
| export class Gun { | |||
| readonly serialNumber: string = 's00001'; | |||
There was a problem hiding this comment.
присваиваете значение в конструкторе - для счетчика можно завести приватную статическую переменную и исходя из нее увеличивать счетчик и устанавливать соответствующий серийный номер
| constructor(modelName: string, magazine: number, public bullets?: number) { | ||
| this.model = modelName; | ||
| this.magazine = magazine; | ||
| this.bullets = this.bullets < this.magazine && this.bullets >= 0 ? this.bullets : this.magazine; |
There was a problem hiding this comment.
если я передам отрицательное или слишком большое число - то стоит бросать исключение - exception
| this.bullets = this.bullets < this.magazine && this.bullets >= 0 ? this.bullets : this.magazine; | ||
| } | ||
| // инфо :) | ||
| info() { |
There was a problem hiding this comment.
давайте переименуем в toString() : string <- указывайте тип возвращаемого значения
| export class Gun { | ||
| readonly serialNumber: string = 's00001'; | ||
| private aMagazine: number; | ||
| model: string; |
| constructor(modelName: string, magazine: number, public bullets?: number) { | ||
| this.model = modelName; | ||
| this.magazine = magazine; | ||
| this.bullets = this.bullets < this.magazine && this.bullets >= 0 ? this.bullets : this.magazine; |
There was a problem hiding this comment.
для установки лучше бы создать приватный метод и просто его из конструктора вызвать
|
|
||
| shotCount = 0; | ||
|
|
||
| constructor(modelName: string, magazine: number, public bullets?: number) { |
There was a problem hiding this comment.
| constructor(modelName: string, magazine: number, public bullets?: number) { | |
| constructor(modelName: string, magazine: number, protected bullets?: number) { |
| shot(): string { | ||
| if (this.bullets > 0) { | ||
| this.shotCount += 1; | ||
| this.bullets -= 1; |
There was a problem hiding this comment.
создайте get для protected поля bullets
|
|
||
| // проверка магазина | ||
| set magazine(magazine: number) { | ||
| this.aMagazine = magazine >= 0 && magazine < 21 ? magazine : this.aMagazine ?? 20; |
There was a problem hiding this comment.
если указано неверное число патрон - бросайте exception
There was a problem hiding this comment.
trow new Exception(wrong amount of bullets)
| }); | ||
| }); | ||
|
|
||
| describe('Testing gun methods', () => { |
There was a problem hiding this comment.
🔕 different describe test suites could be placed in separate files.
| expect(first.shot()).toEqual('The gun Пистолет 8 fired 3 times'); | ||
| expect(first.shot()).toEqual('The gun Пистолет 8 fired 4 times'); |
There was a problem hiding this comment.
наверное, достаточно было бы 2х вызовов
|
|
||
| describe('Testing gun methods', () => { | ||
| it('The gun is firing', () => { | ||
| const first = new Gun('Пистолет 7', 20, 20); |
There was a problem hiding this comment.
можно объявить gun вне it, но внутри describe - тогда этот gun использовался бы в нескольких тестах
|
Пока поправьте предыдущие review point'ы |
No description provided.